var fphb = {
    setBtnStatus:function () {
        var row = $('#grid_fphb').datagrid('getSelected');
        if (row && row.id) {
            $('#btn_fphb_lead').show();
        } else {
            $('#btn_fphb_lead').hide();
        }
    }
}
$(function () {
    //表格初始加载
    $('#grid_fphb').datagrid({
        url:$.webPath() + "/cwgl/fphb/fphbList",
        toolbar:"#toolbar_fphb",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        pageNumber: 1,
        fit:true,
        rownumbers:true,
        striped:true,
        columns:[[
            {field:'id', title:'ID', hidden:true},
            {field:'gs_qc', title:'公司名称', width:40,align: 'center'},
            {field:'hbrq', title:'合并日期', width:40,align: 'center'},
            {field:'hbdh', title:'合并单号', width:40,align: 'center'},
            {field:'kjqj', title:'会计区间', width:40,align: 'center'},
            {field:'voucherNum', title:'凭证号', width:30,align: 'center'},
            {field:'hbr_xm', title:'合并人', width:30,align: 'center'}
            ]],
        onLoadSuccess:function(){
            fphb.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function(){
            fphb.setBtnStatus();
        }
    });
    //**********按钮触发事件**********
     $('#btn_fphb_lead').click(function(){
         fphb_insert();
     });
   // 取消按钮
    $("#btn_fphb_cancel").click(function () {
        fphb_cancel();
    });
    //查询按钮
    $("#btn_fphb_search").click(function () {
        fphb_search();
    });

    //加载查询条件中的申报公司下拉框
    $("#_gs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});

//凭证导入
function fphb_insert() {
    var row = $('#grid_fphb').datagrid('getSelected');
    if(row){
        var win = $.window({
            title:"发票凭证导入",
            width:850,
            height:470,
            cache:false,
            url:$.webPath() + "/cwgl/fphb/fphb_content?hbjsdId="+row.id,
            modal:true,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $(win).mask();
                        $('#form_fphb_insert').form('submit', {
                            onSubmit:function () {
                                var isValid = $(this).form('validate');
                                if (!isValid) {
                                    $(win).mask('hide');
                                    return false;
                                }
                                return isValid;
                            },
                            success:function (data) {
                                $(win).mask('hide');
                                if(data.responseflag == '1'){
                                    $.messager.show({
                                        title:'提示',
                                        msg:"导入成功",
                                        timeout:3000,
                                        showType:'slide'
                                    });
                                }
                                else $.messager.alert('提示', data.message,"info");
                            }
                        });
                    }
                },
                {"text":'撤销',handler:function(){
                    $(win).mask();
                    $.post($.webPath()+"/cwgl/fphb/revoke_voucher",{hbjsdId:$("#form_fphb_insert input[name='jhbjsdId']").val()||''},function(data){
                        $(win).window('close');
                        if(data.responseflag == '0'){$.messager.alert('提示', data.message,"info");}
                        if(data.responseflag == '1'){
                            $.messager.show({
                                title:'提示',
                                msg:"撤销成功",
                                timeout:3000,
                                showType:'slide'
                            });
                        }
                    })
                }}
            ]
        });
    }else{
        $.messager.alert('提示', '请选择一行数据', 'warning');
    }
}
function fphb_cancel(){
    var row = $('#grid_fphb').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否要解除该合并单', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/cwgl/fphb/fphb_cancel",
                    dataType: 'json',
                    type: 'POST',
                    data: {hbjsdId:row.id},
                    beforeSend: function () {
                        $("#layout_fphb").mask();
                    },
                    success:function(data){
                        $("#layout_fphb").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_fphb').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要取消合并的发票！", "warning");
    }
}

function fphb_search() {
    var gsId = $('#fphb_gs').combotree('getValue') == null ? null : $('#fphb_gs').combotree('getValue');
    var gsJc = $('#fphb_gs').combotree('getText') == null ? null : $('#fphb_gs').combotree('getText');
    var hbrqStart = $('#fphb_sqrq_start').datebox('getValue');
    var hbrqEnd = $('#fphb_sqrq_end').datebox('getValue');
   $('#grid_fphb').datagrid('load',
        {
            voucherNum:$('#fphb_jsdhb_pzh').val(),
            gsId:gsId,
            gsJc:gsJc,
            hbrqStart:hbrqStart,
            hbrqEnd:hbrqEnd,
            hbrName:$('#fphb_hbr').val()
        }
    );
}
